package com.runshu.runshuwulian.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Environment;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.target.Target;
import com.runshu.runshuwulian.MApplication;
import com.runshu.runshuwulian.R;


import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class UtilLoadImage {
    private static String DOC_ER_CODE_IMG = "http://admin.runshutech.com/uploads/qrcode/doctor";

    public static void loadImage(Context ctx, ImageView iv, String url) {
        Glide.with( ctx ).load( url ).placeholder( R.drawable.default_pic ).into( iv );
    }


//    public static void preLoadImg(Context ctx, String url) {
//        if (TextUtils.isEmpty( url )) return;
//        if (!url.contains( "http://" ) && !url.contains( "https://" )) {
//            url = String.format( ConstantHosts.ImgHost, url );
//        }
//        Glide.with( ctx )
//                .asBitmap()
//                .load( url )
//                .diskCacheStrategy( DiskCacheStrategy.ALL )
//                .preload();
//    }
    public static void loadLocalImage(Context ctx, ImageView iv, File file) {
        Glide.with( ctx ).load( file ).placeholder( R.drawable.default_pic ).into( iv );
    }

    public static void loadResImage(Context ctx, ImageView iv, int resId) {
        Glide.with( ctx ).load( resId ).centerCrop().placeholder( R.drawable.default_pic ).into( iv );
    }

//    @NotNull
//    public static String getHostImgUrl(String url) {
//        if (!url.contains("http://")&& !url.contains("https://")) {
//            url = String.format(ConstantHosts.ImgHost, url);
//        }
//        return  url;
//    }

    public static String getErDocCodeUrl(String doctorId) {
        return DOC_ER_CODE_IMG + doctorId + ".jpg";
    }

//    public static void loadSmallImageNormal(Context ctx, ImageView iv, String url) {
//        if (!url.contains( "http://" ) && !url.contains( "https://" )) {
//            url = String.format( ConstantHosts.ImgHost, url );
//        }
//        String fileName = url.substring( url.lastIndexOf( "/" )+1 );
//        String smallUrl = url.substring( 0,url.lastIndexOf( "/" )+1 )+"thumb"+fileName;
//        Glide.with( ctx ).load( smallUrl ).placeholder( com.hyphenate.easeui.R.drawable.ease_default_header_gray ).into( iv );
////        Glide.with( ctx ).load( url ).placeholder( R.drawable. ).into( iv );
//    }

    public static void loadImageNormal(Context ctx, ImageView iv, String url) {
        if (url==null) {
            return;
        }

//        if (!url.contains("http://")&& !url.contains("https://")) {
//            url = String.format(ConstantHosts.ImgHost, url);
//        }
        Glide.with( ctx ).load( url ).placeholder( R.drawable.default_pic ).into( iv );
    }
    public static void loadHeaderImageNormal(Context ctx, ImageView iv, String url) {
        if (url==null) {
            return;
        }
//        if (!url.contains("http://")&& !url.contains("https://")) {
//            url = String.format( ConstantHosts.ImgHost, url);
//        }
        Glide.with( ctx ).load( url ).placeholder( R.drawable.default_pic ).into( iv );
    }
//    //保存网络图片到相册
//    public static void saveImageUrlWithHeader(Context context, String url, OnListener listener) {
//        AsyncTask<String, Integer, Bitmap> asyncTask = new AsyncTask<String, Integer, Bitmap>() {
//            @Override
//            protected Bitmap doInBackground(String... strings) {
//                Bitmap file = null;
//                try {
////                    file = ImageCreater.getAppShareBmp( MApplication.instance.getApkVersionEntity().getQrcode(), strings[0] );
////                    file = Glide.with( MApplication.instance ).load( strings[0] ).downloadOnly( Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL ).get();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//                return file;
//
//            }
//
//            @Override
//            protected void onPostExecute(Bitmap file) {
//                if (file != null) {
////                            saveImageToGallery(context,file.getAbsolutePath());
//                    listener.onSuccess( file );
//                } else {
//                    listener.onFail();
//                }
////                super.onPostExecute( file );
//            }
//        }.execute( MApplication.instance.getLoginInfo().getMember_avatar() );
//    }

    //保存网络图片到相册
    public static void saveImageUrlToGallery(Context context, String url, OnListener listener) {
        AsyncTask<String, Integer, File> asyncTask = new AsyncTask<String, Integer, File>() {
            @Override
            protected File doInBackground(String... strings) {
                File file = null;
                try {
                    file = Glide.with( MApplication.instance ).load( strings[0] ).downloadOnly( Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL ).get();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return file;

            }

            @Override
            protected void onPostExecute(File file) {
                if (file != null) {
//                            saveImageToGallery(context,file.getAbsolutePath());
                    listener.onSuccess( file );
                } else {
                    listener.onFail();
                }
//                super.onPostExecute( file );
            }
        }.execute( url );
    }
    public abstract static class OnListener {
        public abstract void onFail();

        public void onSuccess(File file) {
        }

        ;

        public void onSuccess(Bitmap bitmap) {
        }

        ;

    }


    /**
     *  * 创建原图像保存的文件
     *  *
     *  * @return
     *  * @throws IOException
     *  
     */
    public static File createOriImageFile() {
        String imgNameOri = new SimpleDateFormat( "yyyyMMdd_HHmmss" ).format( new Date() );
        File pictureDirOri = new File( MApplication.getInstance().getExternalFilesDir(
                Environment.DIRECTORY_PICTURES ).getAbsolutePath() + "/OriPicture" );
        if (!pictureDirOri.exists()) {
            pictureDirOri.mkdirs();
        }
        File image = null;
        try {
            image = File.createTempFile( imgNameOri, ".jpg", pictureDirOri );
        } catch (IOException e) {
            e.printStackTrace();
        }
        return image;
    }
}
